<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Workerman\Worker;
use App\Handlers\SwitchHandler;

class mqtt extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mqtt {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'description';

    protected $client_id = 'php-server';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command. 
     *
     * @return mixed
     */
    public function handle()
    {
        //接收参数
        // var_dump($this->argument('name'), $this->option('city'));
        $worker = new Worker();
        $worker->onWorkerStart = function(){
            $mqtt = new \Workerman\Mqtt\Client('mqtt://218.85.129.211:1883',array('username' => 'hf-mqtt','password' => 'drd12345',));
            $mqtt->onConnect = function($mqtt) {
                $mqtt->subscribe('hongfa/FFF2211026162309/upload/');
            };
            $mqtt->onMessage = function($topic, $content){
                var_dump($topic, $content);
                SwitchHandler::analysisContent($topic, $content);                
            };
            $mqtt->connect();
        };
        Worker::runAll();
    }
}
